/*
 * Power by www.xiaoi.com
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author eko.zhan
 * @date Jan 8, 2015 3:29:16 PM
 * @version 1.0
 */
public class DBManager {

	/**
	 * 连接Oracle
	 * @return
	 */
	public static Connection getOracleConnection(){
		String driverName = "oracle.jdbc.driver.OracleDriver";   //加载JDBC驱动 
		String url = "jdbc:oracle:thin:@192.168.126.131:1521:orcl";
		String username = "scott";
		String password = "passw0rd";
		
		Connection conn = null;
		
		try{
			Class.forName(driverName);
			conn = DriverManager.getConnection(url, username, password);
			System.out.println("Oracle 连接成功");
		}catch (Exception e){
			System.out.println("数据库连接出错: " + e.getMessage());
		}
		return conn;
	}
	/**
	 * 连接MySQL
	 * @return
	 */
	public static Connection getMySQLConnection(){
		String driverName = "com.mysql.jdbc.Driver";   //加载JDBC驱动 
		String url = "jdbc:mysql://192.168.126.131:3306/test?useUnicode=true&characterEncoding=UTF-8";
		String username = "root";
		String password = "password";
		
		Connection conn = null;
		
		try{
			Class.forName(driverName);
			conn = DriverManager.getConnection(url, username, password);
			System.out.println("MySQL 连接成功");
		}catch (Exception e){
			System.out.println("数据库连接出错: " + e.getMessage());
		}
		return conn;
	}
	/**
	 * 连接SQL Server
	 * @return
	 */
	public static Connection getSQLServerConnection(){
		String driverName = "net.sourceforge.jtds.jdbc.Driver";   //加载JDBC驱动 
		String url = "jdbc:jtds:sqlserver://192.168.126.131/test" ;
		String username = "sa";
		String password = "passw0rd";
		
		Connection conn = null;
		
		try{
			Class.forName(driverName);
			conn = DriverManager.getConnection(url, username, password);
			System.out.println("SQLServer 连接成功");
		}catch (Exception e){
			System.out.println("数据库连接出错: " + e.getMessage());
		}
		return conn;
	}
	/**
	 * 释放资源
	 * @param conn
	 */
	public static void cleanup(Connection conn){
		cleanup(conn, null, null);
	}
	/**
	 * 释放资源
	 * @param conn
	 * @param stmt
	 */
	public static void cleanup(Connection conn, Statement stmt){
		cleanup(conn, stmt, null);
	}
	/**
	 * 释放资源
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public static void cleanup(Connection conn, Statement stmt, ResultSet rs){
		try {
			if (rs != null) {
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		try {
			if (stmt != null) {
				stmt.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (conn != null && !conn.isClosed()) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
